<!--
 * @Description: 商家分布模块
 * @Author: 梁萌
 * @Date: 2020-12-06 11:16:52
 * @LastEditTime: 2020-12-19 16:49:08
 * @FilePath: \vision\src\components\Map.vue
-->
<template>
  <div class="com-container" @dblclick="revertMap">
    <div class="com-chart" ref="map_ref"></div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import axios from "axios";
import { getProvinceMapInfo } from "@/utils/map_utils";
export default {
  data() {
    return {
      chartIntance: null,
      allData: null, //服务器返回的数据
      mapData: {}, //省份地图矢量数据
    };
  },
  created() {
    //组件创建完成后进行回调函数的注册
    this.$socket.registerCallBack("mapData", this.getData);
  },
  mounted() {
    this.initChart();
    //this.getData();
    //发送数据给服务器,获取数据
    this.$socket.send({
      action: "getData",
      socketType: "mapData",
      chartName: "map",
      value: "",
    });
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    //在组件销毁时进行回调函数取消
    this.$socket.unRegisterCallBack("mapData");
  },
  methods: {
    //初始化echartsInstance对象
    async initChart() {
      this.chartIntance = this.$echarts.init(this.$refs.map_ref, this.theme); //初始化echarts实例对象

      //获取中国地图的矢量数据
      const ret = await axios.get(
        "http://localhost:9999/static/map/china.json"
      );
      this.$echarts.registerMap("china", ret.data);

      //对图表初始化配置的控制
      const initOption = {
        title: {
          text: "▎商家分布",
          left: 20,
          top: 20,
        },
        geo: {
          type: "map",
          map: "china",
          top: "5%",
          bottom: "5%",
          itemStyle: {
            areaColor: "#2E72BF",
            borderColor: "#333",
          },
        },
        legend: {
          left: "5%",
          bottom: "5%",
          orient: "vertical",
        },
      };
      this.chartIntance.setOption(initOption);

      //地图点击事件(显示单个省份地图)
      this.chartIntance.on("click", async (arg) => {
        //调用工具类,获取省份地图矢量数据的文件路径
        const provinceInfo = getProvinceMapInfo(arg.name);
        //如果点击的是省地图上的市,则不再请求数据,解决报错问题
        if (typeof provinceInfo.key == "undefined") {
          return;
        }

        if (!this.mapData[provinceInfo.key]) {
          const proRet = await axios.get(
            "http://localhost:9999" + provinceInfo.path
          );
          this.mapData[provinceInfo.key] = proRet.data;
          this.$echarts.registerMap(provinceInfo.key, proRet.data);
        }

        const changeOption = {
          geo: {
            map: provinceInfo.key,
          },
        };
        this.chartIntance.setOption(changeOption);
      });
    },

    //获取服务器数据
    getData(ret) {
      //const { data: ret } = await this.$http.get("map");
      this.allData = ret;
      this.updateChart();
    },

    //更新图表
    updateChart() {
      //图例数据
      const legendArr = this.allData.map((item) => {
        return item.name;
      });

      //一个类别下的所有散点数据
      const seriesArr = this.allData.map((item) => {
        return {
          type: "effectScatter", //散点图
          coordinateSystem: "geo", //散点基于地图坐标
          rippleEffect: {
            //设置涟漪效果
            scale: 5,
            brushType: "stroke",
          },
          name: item.name,
          data: item.children,
        };
      });

      const dataOption = {
        //配置图例数据
        legend: {
          data: legendArr,
        },

        //配置散点数据
        series: seriesArr,
      };

      this.chartIntance.setOption(dataOption);
    },

    //当浏览器大小变化时进行屏幕适配
    screenAdapter() {
      const titleFontSize = (this.$refs.map_ref.offsetWidth / 100) * 3.6;
      //和分辨率大小相关配置
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        legend: {
          itemWidth: titleFontSize / 2,
          itemHeight: titleFontSize / 2,
          itemGap: titleFontSize / 2,
        },
      };

      this.chartIntance.setOption(adapterOption);
      this.chartIntance.resize(); //图表大小的自适应
    },

    //双击单个省份地图时显示中国地图
    revertMap() {
      const revertOption = {
        geo: {
          map: "china",
        },
      };
      this.chartIntance.setOption(revertOption);
    },
  },
  computed: {
    ...mapState(["theme"]),
  },
  watch: {
    theme() {
      this.chartIntance.dispose();
      this.initChart();
      this.screenAdapter();
      this.updateChart();
    },
  },
};
</script>

<style>
</style>